import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.lang.StringBuffer;

class HttpClient 
{ 
	private BufferedReader inputStream = null; 
    
	public String GETRequest(String url) throws MalformedURLException, UnknownHostException, IOException
	{
		getInputStream(url);
		String result = getResponse(inputStream);
		closeConnections();
		
		return result;
	}           
  
	public void getInputStream(String url) throws MalformedURLException, UnknownHostException, IOException
	{ 
		inputStream = null; 
		String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6";

		try
		{
			URL goog = new URL(url);
			try 
			{
				Thread.sleep(100);
			} 
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			URLConnection conn = goog.openConnection();
			conn.setDoOutput(true);
			conn.setRequestProperty("User-Agent", userAgent);
			conn.setRequestProperty("Accept", "text/html");
			conn.setRequestProperty("Referer", "http://scholar.google.com");
			conn.connect();
			inputStream = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		} 
		catch(SocketException e)
		{
			System.err.println(e);
			e.printStackTrace();
			System.exit(0);
		}
		catch (MalformedURLException e)
		{
			System.err.println(e);
	    }
		catch (UnknownHostException e) 
		{
			System.err.println(e);
		} 
		catch (IOException e) 
		{
			System.err.println(e);
		}
	
	}   

	public String getResponse(BufferedReader in) throws IOException
	{       
		String responseLine = "";
		StringBuffer stringBuffer = new StringBuffer();
		
		while (true)
		{
			try
			{
				responseLine = in.readLine();
			}
			catch (IOException e)
			{
				System.err.println(e);
			}
			if (responseLine == null)
			{
				break;
			}
			
			stringBuffer.append(responseLine);
		}
		
		return stringBuffer.toString();
	}
      
      
            
	public void closeConnections()
	{  
		try{inputStream.close();}
		catch(Exception e){}
		finally{inputStream = null;}
	}
	
	public static void main(String [] args)
	{
		HttpClient hc = new HttpClient();
		String query = "http://scholar.google.com/scholar?as_q=university+of+limerick&num=100&as_sauthors=bannon++liam&as_subj=eng";
		try 
		{
			StringBuffer test = new StringBuffer(hc.GETRequest(query));
			GoogleParser gp = new GoogleParser(test);
			System.out.println(gp.getPageHighest());
		} 
		catch (MalformedURLException e) 
		{
			e.printStackTrace();
		} catch (UnknownHostException e) 
		{
			e.printStackTrace();
		} catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
}
